package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Dept;
import db.dao.DeptDAO;
import db.dao.Teacher;
import db.dao.TeacherDAO;
import db.dao.Users;
import db.dao.UsersDAO;
import hibernate.HibernateSessionFactory;
/*添加教师*/
public class AddTeacherAction extends ActionSupport{
	private String tid="";	//教师编号
	private String tname="";	//教师姓名
	private String tsex="";	//教师性别
	private String tiden="";	//教师身份证号
	private String tdept="";	//教师所属学院
	
	public AddTeacherAction() {
	}

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public String getTname() {
		return tname;
	}

	public void setTname(String tname) {
		this.tname = tname;
	}

	public String getTsex() {
		return tsex;
	}

	public void setTsex(String tsex) {
		this.tsex = tsex;
	}

	public String getTiden() {
		return tiden;
	}

	public void setTiden(String tiden) {
		this.tiden = tiden;
	}
	
	public String getTdept() {
		return tdept;
	}

	public void setTdept(String tdept) {
		this.tdept = tdept;
	}
	//更新用户表
	public void updateUsers() {
		//设置参数
		UsersDAO uDao = new UsersDAO();
		Users users = new Users();
		users.setUsername(tid);
		users.setUserpwd(tid);
		users.setUsertype("教师");
		//保存
		uDao.save(users);
	}
	
	public String execute() throws IOException{
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		Teacher teacher = new Teacher();
		TeacherDAO tDao = new TeacherDAO();
		//查找所属学院
		DeptDAO dDao = new DeptDAO();
		Dept dept = new Dept();
		dept = dDao.findById(tdept);
		//设置教师参数
		teacher.setTeaid(tid);
		teacher.setTeaname(tname);
		teacher.setTeaidentify(tiden);
		//判空
		if (!tsex.equals("")) {
			teacher.setTeasex(tsex);
		}
		if (dept!=null) {
			teacher.setDept(dept);
		}
		result = tDao.save(teacher);
		if (result.equals("success")) {
			//更新用户表
			updateUsers();
			//成功弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加成功!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加失败!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
